package cn.xshi.oauth.web;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import cn.xshi.oauth.model.OauthFunctionCommon;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.xshi.common.annotation.Auth;
import cn.xshi.common.base.BaseAction;
import cn.xshi.common.base.BasePage;
import cn.xshi.common.base.BaseResult;
import cn.xshi.common.base.BaseSearch;
import cn.xshi.common.entity.OauthAccountEntity;
import cn.xshi.common.util.StringUtil;
import cn.xshi.oauth.service.OauthFunctionCommonService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageInfo;
import javax.annotation.Resource;
/**
 * @Desc 授权中心公共功能
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/oauthFunctionCommon")
@Api(value = "授权中心公共功能API",tags = "授权中心公共功能API",description = "授权中心公共功能API")
public class OauthFunctionCommonController extends BaseAction {

	@Resource
	private OauthFunctionCommonService oauthFunctionCommonService;

	/**
	* 查询公共功能列表并分页
	* @param baseSearch 
	*/
	@ApiOperation(value="查询公共功能列表并分页", notes="查询公共功能列表并分页")
	@PostMapping(value="/list")
	@Auth(value = "/oauthFunctionCommon/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BasePage<List<OauthFunctionCommon>> getOauthFunctionCommonListByCondition(@RequestBody BaseSearch baseSearch){
		Map<String, Object> condition = baseSearch.convert();
		commonHPager(baseSearch);
		List<OauthFunctionCommon> oauthFunctionCommonList = oauthFunctionCommonService.getOauthFunctionCommonListByCondition(condition);
		for(OauthFunctionCommon oauthFunctionCommon:oauthFunctionCommonList){
			if(!StringUtil.isEmpty(oauthFunctionCommon.getCreateId())){
				OauthAccountEntity createBy = getAccount(oauthFunctionCommon.getCreateId());
				if(null != createBy){
					oauthFunctionCommon.setCreateBy(createBy.getName());
				}
			}
			if(!StringUtil.isEmpty(oauthFunctionCommon.getUpdateId())){
				OauthAccountEntity modifiedBy = getAccount(oauthFunctionCommon.getUpdateId());
				if(null != modifiedBy){
					oauthFunctionCommon.setModifiedBy(modifiedBy.getName());
				}
			}
		}
		PageInfo<OauthFunctionCommon> page = new PageInfo<OauthFunctionCommon>(oauthFunctionCommonList);
		return outPageBootStr(page,baseSearch);
	}

	/**
	* 查询单个公共功能
	* @param id
	*/
	@ApiOperation(value="查询单个公共功能", notes="查询单个公共功能")
	@GetMapping(value="/get/{id}")
	@Auth(value = "/oauthFunctionCommon/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<OauthFunctionCommon> getOauthFunctionCommonById(@PathVariable("id")String id){
		OauthFunctionCommon oauthFunctionCommon = oauthFunctionCommonService.getOauthFunctionCommonById(id);
		if(!StringUtil.isEmpty(oauthFunctionCommon.getCreateId())){
			OauthAccountEntity createBy = getAccount(oauthFunctionCommon.getCreateId());
			if(null != createBy){
				oauthFunctionCommon.setCreateBy(createBy.getName());
			}
		}
		if(!StringUtil.isEmpty(oauthFunctionCommon.getUpdateId())){
			OauthAccountEntity modifiedBy = getAccount(oauthFunctionCommon.getUpdateId());
			if(null != modifiedBy){
				oauthFunctionCommon.setModifiedBy(modifiedBy.getName());
			}
		}
		return outDataStr(oauthFunctionCommon);
	}

	/**
	* 添加
	* @param oauthFunctionCommon 
	*/
	@PostMapping(value="/add")
	@ApiOperation(value="创建单个公共功能", notes="创建单个公共功能")
	public BaseResult addOauthFunctionCommon(@RequestBody OauthFunctionCommon oauthFunctionCommon){
		int i = 0;
		if(null != oauthFunctionCommon){
			i=oauthFunctionCommonService.addOauthFunctionCommon(oauthFunctionCommon);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	* 修改
	* @param oauthFunctionCommon 
	*/
	@ApiOperation(value="编辑单个公共功能", notes="编辑单个公共功能")
	@PutMapping(value="/update")
	public BaseResult updateOauthFunctionCommon(@RequestBody OauthFunctionCommon oauthFunctionCommon){
		int i = 0;
		if(null != oauthFunctionCommon){
			i=oauthFunctionCommonService.updateOauthFunctionCommon(oauthFunctionCommon);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	* 删除
	* @param id
	*/
	@ApiOperation(value="删除公共功能", notes="删除公共功能")
	@DeleteMapping(value="/delete")
	public BaseResult delOauthFunctionCommon(String id){
		int i = 0;
		if(!StringUtil.isEmpty(id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("id",id.split(","));
			i=oauthFunctionCommonService.delOauthFunctionCommon(condition);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}
}
